/**
 * 
 */
package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.net.GameProtocol;
import com.handinfo.game.AbstractGameActor;
import com.handinfo.game.ActorItem;
import com.handinfo.game.ActorRole;
import com.handinfo.game.GameMacro;
import com.handinfo.game.GameScene;
import com.handinfo.game.MenuFunction;
import com.handinfo.game.RoleCombatSkill;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

/**
 * @author venizeng
 * 
 */
public class UISetMarco extends UIAbstractWindow
{
	private final String TITLE = "自定义连招";
	private final String STR_REVIEW = "点击查看" ;
	private final String STR_DESCRIBE1 = "选择自定义连招框，在技能里面选择。" ;
	private final String STR_TITLE_DATA = "当前技能";
	// 显示
	private final int NUM_ITEM_ROW = 4;//
	private final int NUM_ITEM_COL = GameMacro.AMOUNT_MACRO_ACTION;
	private int NUM_DATA_ROW;
	private final int NUM_DATA_COL = 8;
	//
	private final int GROUP_ITEM = 1;
	private final int GROUP_DATA = 2;
	//
	private final int INDEX_ITEM = 1;
	private final int INDEX_DATA = INDEX_ITEM + (NUM_ITEM_ROW * NUM_ITEM_COL);
	//
	private Object[][] m_items;// 每个宏对应的选项（技能，功能）
	private Object[] m_datas;// 宏对应的数据（技能，功能 …… ）
	private int m_index_item;
	// data的位置索引及坐标索引
	private int m_index_top_data;
	private int m_plan;
	private int m_x_fos;
	private int m_y_fos;
	//
	private final int W_MARCO = UITools.W_BAG;
	private final int H_MARCO = UITools.H_BAG;
	private final int IH_MARCO = 3;
	private final int W_ITEM = UITools.W_BAG;
	private final int H_ITEM = UITools.H_BAG;
	private final int IW_ITEM = 4;
	private final int IH_ITEM = IH_MARCO;
	private final int W_DATA = UITools.W_BAG;
	private final int H_DATA = UITools.H_BAG;
	private final int IW_DATA = 3;
	private final int IH_DATA = 3;
	private int RH_MARCO = H_MARCO + IH_MARCO;
	private int RW_ITEM = W_ITEM + IW_ITEM;
	private int RH_ITEM = H_ITEM + IH_ITEM;
	private int RW_DATA = W_DATA + IW_DATA;
	private int RH_DATA = H_DATA + IH_DATA;
	//
	private int X_RECT_MARCO;
	private int Y_RECT_MARCO;
	private int W_RECT_MARCO;
	private int H_RECT_MARCO;

	private int X_MARCO;
	private int Y_MARCO;

	private int X_ITEM;
	private int Y_ITEM;

	private int X_DESCRIBE;
	private int Y_DESCRIBE;
	private int W_DESCRIBE;
	private int H_DESCRIBE;

	private int X_RECT_DATA;
	private int Y_RECT_DATA;
	private int W_RECT_DATA;
	private int H_RECT_DATA;

	//

	/**
	 * @param id
	 */
	public UISetMarco(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		initData();
		initDis();
		setWindowIndex(INDEX_ITEM);
	}

	private void initData()
	{
		ActorRole role = GameManager.getInstance().m_role;
		// 初始化设置栏
		GameMacro[] gameMacros = role.m_game_macro;
		m_items = new Object[role.m_game_macro.length][NUM_ITEM_COL];
		for (int i = 0; i < m_items.length; i++) {
			GameMacro gameMacro = gameMacros[i];
			for (int j = 0; j < m_items[i].length; j++) {
				m_items[i][j] = role.getDataByTypeAndId((int) gameMacro.m_actionId[j][GameMacro.MACRO_INDEX_TYPE],
						gameMacro.m_actionId[j][GameMacro.MACRO_INDEX_ID]);
			}
		}
		// 初始化背包栏
		RoleCombatSkill[] skills = role.m_roleCombatSkill[0];
		if (skills == null) {
			m_datas = new Object[0];// 屏蔽null
		}
		else {
			int num = 0;
			for (int i = 0; i < skills.length; i++) {
				if (RoleCombatSkill.isFitforSet(skills[i])) {
					num++;
				}
			}
			if (num == 0) {
				m_datas = new Object[0];// 屏蔽null
			}
			else {
				int index = 0;
				m_datas = new Object[num];
				for (int i = 0; i < skills.length; i++) {
					if (RoleCombatSkill.isFitforSet(skills[i])) {
						m_datas[index] = skills[i];
						index++;
					}
				}
			}
		}
		// 设置起始位置
		m_index_item = INDEX_ITEM;// 设置栏选中索引
	}

	private void initDis()
	{
		ActorRole role = GameManager.getInstance().m_role;

		X_RECT_MARCO = UITools.INITX_CONTENT;
		Y_RECT_MARCO = UITools.INITY_CONTENT;
		W_RECT_MARCO = UITools.W_RECT;
		H_RECT_MARCO = role.m_game_macro.length * RH_MARCO - IH_MARCO + (UITools.IH_RECT_CONTENT << 1);

		X_MARCO = X_RECT_MARCO + UITools.IW_RECT_CONTENT;
		Y_MARCO = Y_RECT_MARCO + UITools.IH_RECT_CONTENT;

		X_ITEM = X_MARCO + W_MARCO + (UITools.IW_RECT_CONTENT << 1);
		Y_ITEM = Y_MARCO;

		X_DESCRIBE = X_RECT_MARCO;
		Y_DESCRIBE = Y_RECT_MARCO + H_RECT_MARCO + UITools.IH_RECTS;
		W_DESCRIBE = UITools.W_RECT;
		H_DESCRIBE = UITools.H_LABEL + AbstractGameScreen.FONT_H + (UITools.IH_RECT_CONTENT << 1);

		X_RECT_DATA = X_RECT_MARCO;
		Y_RECT_DATA = Y_DESCRIBE + H_DESCRIBE + UITools.IH_RECTS;
		W_RECT_DATA = UITools.W_RECT_DRAG;

		int h = UITools.H_RECT - H_RECT_MARCO - H_DESCRIBE - (UITools.IH_RECTS << 1) - (UITools.IH_RECT_CONTENT << 1);// 剩余H
		NUM_DATA_ROW = (h + IH_DATA) / RH_DATA;
		H_RECT_DATA = NUM_DATA_ROW * RH_DATA - IH_DATA + (UITools.IW_RECT_CONTENT << 1);
	}

	public static void recvMessage(int orderId, DataInputStream dis) throws IOException
	{
		UISetMarco marco = (UISetMarco) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_SET_MARCOS);
		if (marco == null) {
			return;
		}
		switch (orderId) {
			case GameProtocol.CS_SET_MARCO:
				marco.recvSetMarcoConfig(dis);
				break;
		}
	}

	private void recvSetMarcoConfig(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvSetMarcoConfig ");
		m_isOperLocked = false;
		short state = dis.readShort();
		String info = dis.readUTF();
		if (state == 0) {
			if (!info.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
			}
		}
		else {
			setMarcoItems(m_items);
			UIManager.getInstance().removeWindow(this);
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "连招设置成功");
		}
	}

	/**
	 * 修改宏
	 * 
	 * @param items
	 */
	private void setMarcoItems(Object[][] items)
	{
		ActorRole role = GameManager.getInstance().m_role;
		for (int i = 0; i < items.length; i++) {
			for (int j = 0; j < items[i].length; j++) {
				if (items[i][j] == null) {
					role.m_game_macro[i].m_actionId[j][GameMacro.MACRO_INDEX_TYPE] = AbstractGameActor.ACTION_TYPE_NONE;
					role.m_game_macro[i].m_actionId[j][GameMacro.MACRO_INDEX_ID] = 0 ;
				}
				else {
					if (items[i][j] instanceof ActorItem) {
						ActorItem actorItem = (ActorItem) items[i][j];
						role.m_game_macro[i].m_actionId[j][GameMacro.MACRO_INDEX_TYPE] = AbstractGameActor.ACTION_TYPE_ITEM;
						role.m_game_macro[i].m_actionId[j][GameMacro.MACRO_INDEX_ID] = actorItem.m_guid;
					}
					else if (items[i][j] instanceof RoleCombatSkill) {
						RoleCombatSkill skill = (RoleCombatSkill) items[i][j];
						role.m_game_macro[i].m_actionId[j][GameMacro.MACRO_INDEX_TYPE] = AbstractGameActor.ACTION_TYPE_SKILL;
						role.m_game_macro[i].m_actionId[j][GameMacro.MACRO_INDEX_ID] = skill.m_id;
					}
					else if (items[i][j] instanceof MenuFunction) {
						MenuFunction funcation = (MenuFunction) items[i][j];
						role.m_game_macro[i].m_actionId[j][GameMacro.MACRO_INDEX_TYPE] = AbstractGameActor.ACTION_TYPE_FUNCTION;
						role.m_game_macro[i].m_actionId[j][GameMacro.MACRO_INDEX_ID] = funcation.getId();
					}
				}
			}
		}
	}

	protected void actionOnIndexChanged()
	{
		switch (m_groupId) {
			case GROUP_ITEM:
				m_index_item = m_index;
				//
				m_x_fos = X_ITEM + (m_groupIndex % NUM_ITEM_COL) * RW_ITEM;
				m_y_fos = Y_ITEM + (m_groupIndex / NUM_ITEM_COL) * RH_ITEM;
				break;
			case GROUP_DATA:
				int index = m_index_item - INDEX_ITEM;
				int dataIndex = m_index_top_data + m_groupIndex;
				if (dataIndex >= m_datas.length) {//
					m_items[index / NUM_ITEM_COL][index % NUM_ITEM_COL] = null;
				}
				else {
					m_items[index / NUM_ITEM_COL][index % NUM_ITEM_COL] = m_datas[dataIndex];
				}
				m_x_fos = X_RECT_DATA + UITools.IW_RECT_CONTENT + (m_groupIndex % NUM_DATA_COL) * RW_DATA;
				m_y_fos = Y_RECT_DATA + UITools.IH_RECT_CONTENT + (m_groupIndex / NUM_DATA_COL) * RH_DATA;
				break;
		}
	}

	protected int countGroupId(int mIndex)
	{
		if (mIndex < INDEX_DATA) {
			return GROUP_ITEM;
		}
		else {
			return GROUP_DATA;
		}
	}

	protected int countGroupIndex(int mGroupId, int mIndex)
	{
		switch (mGroupId) {
			case GROUP_ITEM:
				return mIndex - INDEX_ITEM;
			case GROUP_DATA:
			default:
				return mIndex - INDEX_DATA;
		}
	}

	/**
	 * 获得最大的顶部索引 2012-4-1 下午04:28:53 venizeng
	 * @return
	 */
	private int getLargestTopIndx()
	{
		if (m_datas.length <= NUM_DATA_ROW) {
			return 0;
		}
		return (m_datas.length - NUM_DATA_ROW) * NUM_DATA_COL;
	}

	/**
	 * 2012-4-1 下午04:27:30 venizeng
	 * @param i
	 */
	private void setTopIndex(int i)
	{
		m_index_top_data = i;
	}

	protected void keyLogic()
	{
		if (m_isOperLocked) {
			return;
		}
		switch (m_groupId) {
			case GROUP_ITEM:
				keyOnItem();
				break;
			case GROUP_DATA:
				keyOnData();
				break;
		}
	}

	private void keyOnItem()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_groupIndex < NUM_ITEM_COL) {// 第一行
				setWindowIndex(m_index + (NUM_ITEM_ROW - 1) * NUM_ITEM_COL);
			}
			else {
				setWindowIndex(m_index - NUM_ITEM_COL);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			if (m_groupIndex >= (NUM_ITEM_ROW - 1) * NUM_ITEM_COL) {// 最后一行
				setWindowIndex(m_index - (NUM_ITEM_ROW - 1) * NUM_ITEM_COL);
			}
			else {
				setWindowIndex(m_index + NUM_ITEM_COL);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
			if (m_groupIndex % NUM_ITEM_COL == 0) {// 第一列
				setWindowIndex(m_index + (NUM_ITEM_COL - 1));
			}
			else {
				setWindowIndex(m_index - 1);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
			if (m_groupIndex % NUM_ITEM_COL == (NUM_ITEM_COL - 1)) {// 最后一列
				setWindowIndex(m_index - (NUM_ITEM_COL - 1));
			}
			else {
				setWindowIndex(m_index + 1);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			clickFire();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			clickLeftSoft();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			clickRightSoft();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY0)) {
			clickReview();
		}
	}

	private void keyOnData()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_groupIndex < NUM_DATA_COL) {// 第一行
				if (m_index_top_data != 0) {
					setTopIndex(m_index_top_data - NUM_DATA_COL);
				}
			}
			else {
				setWindowIndex(m_index - NUM_ITEM_COL);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			if (m_groupIndex >= NUM_DATA_COL * (NUM_DATA_ROW - 1)) {// 最后一行
				if (m_index_top_data != getLargestTopIndx()) {
					setTopIndex(m_index_top_data + NUM_DATA_COL);
				}
			}
			else {
				setWindowIndex(m_index + NUM_DATA_COL);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
			if (m_groupIndex % NUM_DATA_COL == 0) {
				setWindowIndex(m_index + (NUM_DATA_COL - 1));
			}
			else {
				setWindowIndex(m_index - 1);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
			if (m_groupIndex % NUM_DATA_COL == (NUM_DATA_COL - 1)) {// 最后一列
				setWindowIndex(m_index - (NUM_DATA_COL - 1));
			}
			else {
				setWindowIndex(m_index + 1);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			clickFire();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			clickLeftSoft();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			clickRightSoft();
		}else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY0)) {
			clickReview();
		}
	}

	private boolean m_isOperLocked;

	// 点击左软键
	private void clickLeftSoft()
	{
//		switch (m_groupId) {
//			case GROUP_ITEM:
				final UIMessageBox messageBox = (UIMessageBox) UIWindowFactory
						.createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
				final boolean isChanged = isMarcoInfoChanged();
				messageBox.setMessage( getCommitStr(true, isChanged) );
				messageBox.addListener(new UIListener() {

					public void actionPerformed()
					{
						if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
								|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
							if (isChanged) {
								boolean isNetLock = GameManager.getInstance().m_game_message.sendSetMarco(m_items);
								if (isNetLock) {
									GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B,
											"网络繁忙，请稍后重联");
								}
								else {
									m_isOperLocked = true;
								}
							}
							else {
								close();
							}
							UIManager.getInstance().removeWindow(messageBox);
						}
						else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
							UIManager.getInstance().removeWindow(messageBox);
						}
					}
				});
				UIManager.getInstance().addWindow(messageBox);
//				break;
//			case GROUP_DATA:
//				clickFire();
//				break;
//		}
	}

	// 点击右软键
	private void clickRightSoft()
	{
//		switch (m_groupId) {
//			case GROUP_ITEM:
				final UIMessageBox messageBox = (UIMessageBox) UIWindowFactory
						.createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
				boolean isChanged = isMarcoInfoChanged();
				messageBox.setMessage( getCommitStr(false, isChanged) );
				messageBox.addListener(new UIListener() {

					public void actionPerformed()
					{
						if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
								|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
							close();
							UIManager.getInstance().removeWindow(messageBox);
						}
						else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
							UIManager.getInstance().removeWindow(messageBox);
						}
					}
				});
				UIManager.getInstance().addWindow(messageBox);
//				break;
//			case GROUP_DATA:
//				clickFire();
//				break;
//		}
	}
   
	private String getCommitStr(boolean isLeft, boolean isChanged)
	{
		if (isLeft) {
			if (isChanged) {
				return "设置发生修改，确定提交?";
			}
			else {
				return "设置未发生修改，确定提交?";
			}
		}
		else {
			if (isChanged) {
				return "设置发生修改，确定退出?";
			}
			else {
				return "设置未发生修改，确定退出?";
			}
		}
	}

	private boolean isMarcoInfoChanged()
	{
		ActorRole role = GameManager.getInstance().m_role;
		GameMacro[] gameMacros = role.m_game_macro;
		for (int i = 0; i < m_items.length; i++) {
			long[][] actions = gameMacros[i].m_actionId;
			for (int j = 0; j < m_items[i].length; j++) {
				long type = actions[j][GameMacro.MACRO_INDEX_TYPE];
				long id = actions[j][GameMacro.MACRO_INDEX_ID];
				if (m_items[i][j] == null) {// 无数据
					if (type != AbstractGameActor.ACTION_TYPE_NONE) {
						return true;
					}
				}
				else {
					if (m_items[i][j] instanceof RoleCombatSkill) {
						RoleCombatSkill skill = (RoleCombatSkill) m_items[i][j];
						if (type != AbstractGameActor.ACTION_TYPE_SKILL || id != skill.m_id) {
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	private void clickFire()
	{
		switch (m_groupId) {
			case GROUP_ITEM:
				setTopIndex(0);
				setWindowIndex(INDEX_DATA);
				break;
			case GROUP_DATA:
				setWindowIndex(m_index_item);
				break;
		}
	}

	/**
	 * 2012-4-5 下午05:22:14 venizeng
	 */
	private void clickReview()
	{
		Object data = null;
		int dataIndex = 0;
		switch (m_groupId) {
			case GROUP_ITEM:
				dataIndex = getItemDataIndex();
				data = m_items[dataIndex / NUM_ITEM_COL][dataIndex % NUM_ITEM_COL];
				break;
			case GROUP_DATA:
				dataIndex = getDataIndex();
				if (dataIndex == -1) {
					return;
				}
				data = m_datas[dataIndex];
				break;
		}
		if (data == null) {
			return;
		}
		if (data instanceof RoleCombatSkill) {
			RoleCombatSkill skill = (RoleCombatSkill) data;
			GameManager.getInstance().m_game_message.sendSkillDetail(skill.m_id);
		}
	}

	private int getItemDataIndex()
	{
		return m_index_item - INDEX_ITEM;
	}

	/**
	 * 获得技能栏道具 2012-4-5 下午05:25:39 venizeng
	 * @return
	 */
	private int getDataIndex()
	{
		if (m_datas == null) {
			return -1 ;
		}
		int index = m_index_top_data + m_groupIndex;
		if (index >= m_datas.length) {
			return -1;
		}
		else {
			return index;
		}
	}

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, TITLE,STR_REVIEW,true,true);
		paintMarcoRect(g);
		paintMarcos(g);
		paintItems(g);
		paintDescribe(g);
		paintDatas(g);
		paintScrollbar(g);
		paintSetFocus(g);
	}

	private void paintMarcoRect(Graphics g)
	{
		int tempX = m_x + X_RECT_MARCO;
		int tempY = m_y + Y_RECT_MARCO;
		UITools.paintContentRect(g, tempX, tempY, W_RECT_MARCO, H_RECT_MARCO);
	}

	/**
	 * 绘制宏数据 2012-3-31 上午09:59:58 venizeng
	 * @param g
	 */
	private void paintMarcos(Graphics g)
	{
		int tempX = m_x + X_MARCO;
		int tempY = m_y + Y_MARCO;
		GameMacro[] gameMacros = GameManager.getInstance().m_role.m_game_macro;
		for (int i = 0; i < gameMacros.length; i++) {
			paintMarco(g, tempX, tempY, gameMacros[i], i + 1);
			tempY += RH_MARCO;
		}
	}

	private void paintMarco(Graphics g, int x, int y, GameMacro marco, int index)
	{
		g.drawImage(UIManager.IMAGE_ITEM_OPEN, x, y, 0);
		if (marco == null) {
			return;
		}
		int w = GameScene.SIZE_IMG_ICON[0];
		int h = GameScene.SIZE_IMG_ICON[1];
		x += ((UITools.W_BAG - w) >> 1);
		y += ((UITools.H_BAG - h) >> 1);
		marco.drawMacro(g, x, y, index);
	}

	private void paintItems(Graphics g)
	{
		int tempX = m_x + X_ITEM;
		int tempY = m_y + Y_ITEM;
		final int DATAX = tempX;
		int index = 0;
		for (int i = 0; i < NUM_ITEM_ROW; i++) {
			tempX = DATAX;
			for (int j = 0; j < NUM_ITEM_COL; j++) {
				paintData(g, tempX, tempY, m_items[i][j]);
				//
				if (getItemDataIndex() == index) {
					if (m_groupId == GROUP_ITEM) {
						UITools.paintFocusBox(g, tempX, tempY, W_ITEM, H_ITEM);
					}
					else {
						UITools.paintSelItem(g, tempX, tempY, W_ITEM, H_ITEM);
					}
				}
				tempX += RW_ITEM;
				index++;
			}
			tempY += RH_ITEM;
		}
	}

	private void paintDescribe(Graphics g)
	{
		int tempX = m_x + X_DESCRIBE;
		int tempY = m_y + Y_DESCRIBE;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		g.drawString(STR_DESCRIBE1, AbstractGameScreen.SCREEN_WIDTH >> 1, tempY, Tools.HCENTER_TOP);
		tempY += AbstractGameScreen.FONT_H;
		UITools.paintLabelStr(g, tempX, tempY, W_DESCRIBE, UITools.H_LABEL, STR_TITLE_DATA, true);
	}

	private void paintDatas(Graphics g)
	{
		int tempY = m_y + Y_RECT_DATA;
		int tempX = m_x + X_RECT_DATA;
		UITools.paintContentRect(g, tempX, tempY, W_RECT_DATA, H_RECT_DATA);
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		g.setClip(tempX, tempY, W_RECT_DATA - (UITools.IW_RECT_CONTENT << 1), H_RECT_DATA - (UITools.IW_RECT_CONTENT << 1));
		final int DATAX = tempX;
		int index = m_index_top_data;
		for (int i = 0; i < NUM_DATA_ROW; i++) {
			tempX = DATAX;
			for (int j = 0; j < NUM_DATA_COL; j++) {
				if (index >= m_datas.length) {
					paintData(g, tempX, tempY, null);
				}
				else {
					paintData(g, tempX, tempY, m_datas[index]);
				}
				if (m_groupId == GROUP_DATA && m_groupIndex == index - m_index_top_data) {
					UITools.paintFocusBox(g, tempX, tempY, W_DATA, H_DATA);
				}
				tempX += RW_DATA;
				index++;
			}
			tempY += RH_DATA;
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}

	// 绘制数据
	private void paintData(Graphics g, int x, int y, Object data)
	{
		// 绘制单元格子
		g.drawImage(UIManager.IMAGE_ITEM_OPEN, x, y, 0);
		x += ((UITools.W_BAG - GameScene.SIZE_IMG_ICON[0]) >> 1);
		y += ((UITools.H_BAG - GameScene.SIZE_IMG_ICON[1]) >> 1);
		if (data instanceof RoleCombatSkill) {
			RoleCombatSkill skill = (RoleCombatSkill) data;
			skill.drawIcon(g, x, y, 0);
		}
		else if (data instanceof MenuFunction) {
			MenuFunction function = (MenuFunction) data;
			function.drawIcon(g, x, y);
		}

	}

	private void paintScrollbar(Graphics g)
	{
		UITools.paintScrollBar(g, m_x + UITools.X_DRAG, m_y + Y_RECT_DATA, H_RECT_DATA, m_plan);
	}

	private void paintSetFocus(Graphics g)
	{
		int tempX = m_x + m_x_fos;
		int tempY = m_y + m_y_fos;
		switch (m_groupId) {
			case GROUP_ITEM:
				UITools.paintItemDescribe(g, tempX, tempY, m_w, m_h, m_items[m_groupIndex / NUM_ITEM_COL][m_groupIndex
						% NUM_ITEM_COL]);
				break;
			case GROUP_DATA:
				int index = getDataIndex();
				if (index == -1) {
					return;
				}
				UITools.paintItemDescribe(g, tempX, tempY, m_w, m_h, m_datas[index]);
				break;
		}
	}
///////////////////////////////////////////////////
	int[][] mkey, mskill;
	protected void registerTouch() {
		clearAllRects();
		registerNormalSoft();
		int tempX = m_x + X_ITEM;
		int tempY = m_y + Y_ITEM;
		int DATAX = tempX;
//		int index = 0;
		int[][] macroRect = new int[NUM_ITEM_ROW*NUM_ITEM_COL][];
		for (int i = 0; i < NUM_ITEM_ROW; i++) {
			tempX = DATAX;
			for (int j = 0; j < NUM_ITEM_COL; j++) {
				macroRect[i*NUM_ITEM_COL+j] = new int[]{tempX, tempY, W_ITEM, H_ITEM};
				tempX += RW_ITEM;
			}
			tempY += RH_ITEM;
		}
		
		mskill = new int[NUM_DATA_ROW*NUM_DATA_COL][];
		tempY = m_y + Y_RECT_DATA;
		tempX = m_x + X_RECT_DATA;
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		DATAX = tempX;
		for (int i = 0; i < NUM_DATA_ROW; i++) {
			tempX = DATAX;
			for (int j = 0; j < NUM_DATA_COL; j++) {
				mskill[i*NUM_DATA_COL+j] = new int[]{tempX, tempY, W_DATA, H_DATA};
				tempX += RW_DATA;
			}
			tempY += RH_DATA;
		}

		initTouchRect(macroRect);
		initTouchRect(mskill);
		initTouchRect(mkey = new int[][]{
				{446,179,18,26},
				{446,232,18,26},
				{207,273,60,22},
		});
	}

	protected void onTouchRect(int[][] rect, int touchid) {
		if (mkey == rect) {
			switch(touchid) {
			case 0:
				GameTouchManager.keyPressedDir(1);
				break;
			case 1:
				GameTouchManager.keyPressedDir(3);
				break;
			case 2:
				GameTouchManager.keyPressedNum(0);
				break;
			}
		} else if (mskill == rect) {
			setWindowIndex(touchid+INDEX_DATA);
//			GameTouchManager.keyPressedFire();
		} else {
			setWindowIndex(touchid+INDEX_ITEM);
//			switch (m_groupId) { }
	}
	}

	

}
